package com.edgclub.edg.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.SearchUserAdapter;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.UserListData;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


public class GzSearchUserActivity extends GzBaseTooolBarActivity implements
        OnClickListener, VolleyListener {

    private XRecyclerView recyclerView;
    private int currPage = 1;
    private int pageSize = 20;
    private SearchUserAdapter searchUserAdapter;
    private UserPrefUtils userPrefUtils;
    private List<User> userList;
    private TextView text_empty;
    private ImageView imageView_empty;
    private LinearLayout layout_empty;
    private String username;

    @Override
    public int getContentView() {
        return R.layout.activity_search_people;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        toolbar1.setVisibility(View.GONE);
        toolbar2.setVisibility(View.VISIBLE);
        search.setOnClickListener(this);
        imgv_delete.setOnClickListener(this);
    }

    @Override
    public void initView() {
        recyclerView = (XRecyclerView) findViewById(R.id.recyclerview);
        layout_empty = (LinearLayout) findViewById(R.id.layout_empty);
        text_empty = (TextView) findViewById(R.id.text_empty);
        imageView_empty = (ImageView) findViewById(R.id.imageView_empty);
        imageView_empty.setVisibility(View.VISIBLE);
        imageView_empty.setImageResource(R.drawable.nomess);
        text_empty.setText("没有符合条件的用户");
        search_src_text.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //该字母首次出现的位置
//                filterData(s.toString());
                if (s.toString().equals("")) {
                    imgv_delete.setVisibility(View.GONE);
                } else {
                    imgv_delete.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void initData() {
        userPrefUtils = new UserPrefUtils(this);
        searchUserAdapter = new SearchUserAdapter(this);
        userList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        searchUserAdapter.setDatas(userList);
        recyclerView.setAdapter(searchUserAdapter);
        recyclerView.setLoadingMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
//        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
//            @Override
//            public void onRefresh() {
//                currPage = 1;
//                getmembers();
//            }
//
//            @Override
//            public void onLoadMore() {
//                getmembers();
//            }
//        });
//        getmembers();
    }

    private void getmembers() {
        MessageParam messageParam = new MessageParam();
        messageParam.setUserId(userPrefUtils.getUser().getId() + "");
        messageParam.setUsername(username);
        new FriendApi(this).userSearch(messageParam, this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.search:
                username = search_src_text.getText().toString().trim();
                if ("".equals(username)) {
                    Toast.makeText(this, "请输入用户昵称或Eid", Toast.LENGTH_SHORT).show();
                    return;
                }
                showDialog1();
                getmembers();
                break;
            case R.id.imgv_delete:
                search_src_text.setText("");
                break;
            default:
                break;
        }
    }


    @Override
    public void onStart(String tag) {
//        showDialog("正在加载...",false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        if (JSONHelper.isSuccess(response)) {
            UserListData data = JsonUtil.getMode(response,
                    UserListData.class);
            if (null != data && null != data.getData() && data.getData().size() > 0) {
                userList.clear();
                userList.addAll(data.getData());
                searchUserAdapter.notifyDataSetChanged();
            } else {
                recyclerView.noMoreLoading();
            }
            if (!"".equals(userList) && userList.size() > 0) {
                layout_empty.setVisibility(View.GONE);
                recyclerView.setPullRefreshEnabled(true);
            } else {
                layout_empty.setVisibility(View.VISIBLE);

                recyclerView.setPullRefreshEnabled(false);
            }
        }

    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        setNoNetWork(new ReLoadClickListener() {
            @Override
            public void reload() {
                getmembers();
            }
        });
        recyclerView.loadMoreComplete();
        recyclerView.refreshComplete();
        ResultCode.toastVolleyError(this, volleyError);
    }
}
